using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using BLL;
using Common;
using Model;

namespace UI {
    public partial class FrmOrderWithProduct : Form {
        private OrderProductBLL _orderProductBll = new OrderProductBLL();
        private CategoryBLL _categoryBLL = CategoryBLL.GetInstance();
        private ProductBLL _productBll = ProductBLL.GetInstance();
        private RADwithOrderId _raDwithOrderId;
        public FrmOrderWithProduct() {
            InitializeComponent();
        }

        public void InitFrm(RADwithOrderId radWithOrderId) {
            _raDwithOrderId = radWithOrderId;
            label10.Text = radWithOrderId.Room.RoomName;
            label9.Text = radWithOrderId.Desk.DeskName;
        }

        private void FrmOrderWithProduct_Load(object sender, EventArgs e) {
            UpdateProductView();
            UpdateOrderView();
        }

        private void UpdateOrderView() {
            List<OrderProductDTO> dtos = _orderProductBll.QueryAllByOrderId(_raDwithOrderId.OrderId);
            dgvROrderProduct.DataSource = dtos;
            int count = 0;
            decimal money = 0;
            foreach (OrderProductDTO orderProductDto in dtos) {
                count += orderProductDto.ProCount;
                money += orderProductDto.totalPrice;
            }
            lblCount.Text = count.ToString();
            lblMoney.Text = money.ToString();
        }

        private void UpdateProductView() {
            dgvProductInfo.DataSource = _productBll.GetAllProduct();
            //初始化treeview
            DataTable allCategory = _categoryBLL.GetAllCategory();
            //第一层主节点
            foreach (DataRow categoryRow in allCategory.Rows) {
                TreeNode CategoryNode = new TreeNode(categoryRow["CatName"].ToString());
                DataTable products = _productBll.GetProductByCatID(new Product(){CatId = int.Parse(categoryRow["CatId"].ToString())});
                foreach (DataRow productsRow in products.Rows) {
                    CategoryNode.Nodes.Add(productsRow["ProName"].ToString());
                }
                tvCategory.Nodes.Add(CategoryNode);
            }
        }

        //双击添加消费
        private void dgvProductInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            DataGridViewRow dataGridViewRow = dgvProductInfo.CurrentRow;
            int proId = int.Parse(dataGridViewRow.Cells["ProId"].Value.ToString());
            //扣库存
            Result<Product> result = _productBll.DownProduct(new Product() {
                ProId = proId,
                ProStock = double.Parse(dataGridViewRow.Cells["ProStock"].Value.ToString())
            });
            if (!result.IsSuccess) {
                MessageBox.Show("Insufficient inventory");
                return;
            }
            //刷新产品展示
            UpdateProductView();
            //为订单产品表添加
            _orderProductBll.Add(new OrderProduct() {
                OrderId = _raDwithOrderId.OrderId,
                ProId = proId
            });
            //根据订单产品表刷新dgv
            UpdateOrderView();
        }

        private void btnDeleteRorderPro_Click(object sender, EventArgs e) {
            DataGridViewRow dataGridViewRow = dgvROrderProduct.CurrentRow;
            int proId = int.Parse(dataGridViewRow.Cells[0].Value.ToString());
            //扣订单产品数
            Result<OrderProduct> result = _orderProductBll.Deduct(new OrderProduct() {
                OrderId = _raDwithOrderId.OrderId,
                ProId = proId
            });
            if (!result.IsSuccess) {
                MessageBox.Show("Insufficient inventory");
                return;
            }
            //给对应产品库存添加
            _productBll.UpProduct(new Product() {
                ProId = proId,
                ProStock = double.Parse(dgvProductInfo.CurrentRow.Cells["ProStock"].Value.ToString())
            });
            //刷新两个dgv
            UpdateOrderView();
            UpdateProductView();
        }
    }
}